﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Cat;
using System.Security.Cryptography;

namespace Thesis_2
{
    public partial class frmCustomerEntry : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcCommand cmd = null;
        OdbcConnection con = null;
        OdbcDataReader rdr = null;
        public frmCustomerEntry()
        {
            InitializeComponent();
            btnRegister.Click += new EventHandler(btnRegister_Click);
            btnNewRecord.Click += new EventHandler(btnNewRecord_Click);
            btnupdate.Click += new EventHandler(btnupdate_Click);
            btnGetDetails.Click += new EventHandler(btnGetDetails_Click);
            txtContact_no.KeyPress += new KeyPressEventHandler(txtContact_no_KeyPress);
            txtAltContactno.KeyPress += new KeyPressEventHandler(txtAltContactno_KeyPress);
            txtContact_no.Validating+=new CancelEventHandler(txtContact_no_Validating);
            txtAltContactno.Validating += new CancelEventHandler(txtAltContactno_Validating);
            this.Load += new EventHandler(frmCustomerEntry_Load);
        }

        void txtAltContactno_Validating(object sender, CancelEventArgs e)
        {
            if (txtAltContactno.TextLength > 12)
            {
                MessageBox.Show("Only 11 digits are allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContact_no.Focus();
            }
        }
        void txtAltContactno_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void txtContact_no_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void btnGetDetails_Click(object sender, EventArgs e)
        {
            frmCustomerList frmCL = new frmCustomerList();
            frmCL.Show();
            this.Close();
        }
        void frmCustomerEntry_Load(object sender, EventArgs e)
        {
            autoid();
        }
        void btnupdate_Click(object sender, EventArgs e)
        {
            frmCustomerUpdate frmC = new frmCustomerUpdate();
            frmC.Show();
            this.Hide();
        }       
        void reset()
        {
            txtCustomerName.Text = "";
            txtAddress.Text = "";
            txtCity.Text = "";
            txtEmail.Text = "";
            txtAltContactno.Text = "";
            txtContact_no.Text = "";
            
        }
        void btnNewRecord_Click(object sender, EventArgs e)
        {
            reset();
            autoid();
        }
        void txtContact_no_Validating(object sender, CancelEventArgs e)
        {
            if (txtContact_no.TextLength > 12)
            {
                MessageBox.Show("Only 11 digits are allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContact_no.Focus();
            }
        }
        public void autoid()
        {
            txtCustomerID.Text = "CID-" + GetUniqueKey(3);
        
        }
        public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars = "123456789".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
        void btnRegister_Click(object sender, EventArgs e)
        {
            //Error trap with null values
            #region 
            if (txtCustomerName.Text == "")
            {
                MessageBox.Show("Please enter Customer Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCustomerName.Focus();
                return;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Please enter Customer address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAddress.Focus();
                return;
            }
            if (txtCity.Text == "")
            {
                MessageBox.Show("Please enter City", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCity.Focus();
                return;
            }
            if (txtContact_no.Text == "")
            {
                MessageBox.Show("Please enter Contact number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContact_no.Focus();
                
                return;
            }
            if (txtAltContactno.Text == "")
            {
                MessageBox.Show("Please enter Alternate Contact Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAltContactno.Focus();
                
                return;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Please enter Customer Email Adrress", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }
            #endregion // 
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select Customername from tblcustomerprofile where Customername='" + txtCustomerName.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Customer Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCustomerName.Text = "";
                    txtCustomerName.Focus();

                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string cb = "Insert into tblcustomerprofile(CustomerID,Customername,Address,City,Contactno,AltContactno,Email) VALUES ('" + txtCustomerID.Text + "','" + txtCustomerName.Text + "','" + txtAddress.Text + "','" + txtCity.Text + "','" + txtContact_no.Text + "','" + txtAltContactno.Text + "','" + txtEmail.Text+ "')";
                cmd = new OdbcCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully Registered", "Customer", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reset();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
            
            
            }



        }


    }
}
